/*
    TwoPageView image viewer
    Copyright (C) 2006 Japan Manoretimedia Lab.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
namespace TwoPageView
{
    public partial class FormEditBookmarks : System.Windows.Forms.Form
    {
        private UserPreference userPreference;

        public FormEditBookmarks(UserPreference userPreference)
        {
            InitializeComponent();
            if (null == userPreference) {
                throw new System.ArgumentNullException("userPreference");
            }
            this.userPreference = userPreference;

            listBoxBookmarks.Items.Clear();
            string[] bookmarkList = this.userPreference.BookmarkList;
            foreach (string s in bookmarkList) {
                listBoxBookmarks.Items.Add(s);
            }
            buttonUp.Enabled = false;
            buttonDown.Enabled = false;
        }

        private void buttonRemove_Click(object sender, System.EventArgs e)
        {
            for (int i = listBoxBookmarks.Items.Count-1; 0 <= i; --i) {
                if (true == listBoxBookmarks.GetSelected(i)) {
                    listBoxBookmarks.Items.RemoveAt(i);
                }
            }
        }

        private void buttonCancel_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, System.EventArgs e)
        {
            userPreference.BookmarkListClear();
            for (int i = 0; i < listBoxBookmarks.Items.Count; ++i) {
                userPreference.BookmarkAdd(listBoxBookmarks.Items[i].ToString());
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void listBoxBookmarks_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (1 == listBoxBookmarks.SelectedIndices.Count) {
                buttonUp.Enabled = 0 != listBoxBookmarks.SelectedIndex;
                buttonDown.Enabled = (listBoxBookmarks.Items.Count-1) != listBoxBookmarks.SelectedIndex;
            } else {
                buttonUp.Enabled = false;
                buttonDown.Enabled = false;
            }
        }

        private void buttonUp_Click(object sender, System.EventArgs e)
        {
            if (1 != listBoxBookmarks.SelectedIndices.Count
            || 0 == listBoxBookmarks.SelectedIndex) {
                return;
            }
            int pos = listBoxBookmarks.SelectedIndex;
            string sTmp = listBoxBookmarks.Items[pos].ToString();
            listBoxBookmarks.Items.RemoveAt(pos);
            listBoxBookmarks.Items.Insert(pos - 1, sTmp);
            listBoxBookmarks.SelectedIndex = pos - 1;
        }

        private void buttonDown_Click(object sender, System.EventArgs e)
        {
            if (1 != listBoxBookmarks.SelectedIndices.Count
                || (listBoxBookmarks.Items.Count - 1) == listBoxBookmarks.SelectedIndex) {
                return;
            }
            int pos = listBoxBookmarks.SelectedIndex;
            string sTmp = listBoxBookmarks.Items[pos].ToString();
            listBoxBookmarks.Items.RemoveAt(pos);
            listBoxBookmarks.Items.Insert(pos + 1, sTmp);
            listBoxBookmarks.SelectedIndex = pos + 1;
        }
    }
}